package com.mingqijia.gassafety.job.hangzhou.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.mingqijia.gassafety.db.hangzhou.sdk.IoTApiRequest;
import com.mingqijia.gassafety.job.hangzhou.request.*;
import com.mingqijia.gassafety.job.hangzhou.response.HzResponse;
import com.mingqijia.gassafety.job.hangzhou.service.HangZhouService;
import com.mingqijia.gassafety.shared.constant.HangZhouConst;
import com.mingqijia.gassafety.shared.constant.SystemConst;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;

import static com.mingqijia.gassafety.shared.constant.HangZhouConst.*;

/**
 * @program: gassafety
 * @description:
 * @author: Mr.Wang
 * @create: 2022-12-01 10:11
 **/
@Slf4j
@Service
public class HangZhouServiceImpl  implements HangZhouService {

    @Override
    public String setCityDeviceData(CityDeviceDataGeneralRequest req) {
        log.info("setCityDeviceData---start: ");
        String url = HANGZHOU_LOCALIZATION_URL+ HANGZHOU_DATA_SET_URL;
        String response = post(req,url);
        return response;
    }




    @Override
    public HzResponse registerCityDevice(RegisterCityDevice req) {
        log.info("registerCityDevice---start: ");
        String url = HANGZHOU_LOCALIZATION_URL+ HANGZHOU_DEVICE_REGISTER_URL;
        IoTApiRequest request = new IoTApiRequest();
        request.putParam("productKey",req.getProductKey());
        request.putParam("deviceName",req.getDeviceName());
        request.putParam("alias",req.getAlias());
        request.putParam("organizationId", HangZhouConst.ORGANIZATION_ID);
        request.putParam("description",req.getDescription());
        if (req.getDeviceLocation() != null) {
            JSONObject deviceLocation = new JSONObject();
            deviceLocation.put("address",req.getDeviceLocation().getAddress());
            deviceLocation.put("latitude",req.getDeviceLocation().getLatitude());
            deviceLocation.put("coordinateSystem",req.getDeviceLocation().getCoordinateSystem());
            deviceLocation.put("longitude",req.getDeviceLocation().getLongitude());
            request.putParam("deviceLocationDto",deviceLocation);
        }
        log.info("registerCityDevice---request:{}" ,JSONObject.toJSONString(request));
//        HzResponse response = post(request,url);

        return null;
    }




    @Override
    public String createCityProduct(CreateCityProductRequest req) {
        String url = HANGZHOU_LOCALIZATION_URL+HANGZHOU_CREATE_PRODUCT_URL;
        String response = post(req, url);
        return response;
    }

    @Override
    public String createCityThingModel() {
        String url = HANGZHOU_LOCALIZATION_URL+HANGZHOU_THING_MODEL_URL;
        String response = post("null", url);
        return response;
    }

    @Override
    public String setCityDeviceData(CityDeviceDataRequest req) {
        String url = HANGZHOU_LOCALIZATION_URL+ HANGZHOU_ALARM_DATA_SET_URL;
        String response = post(req, url);
        return response;
    }


    /**
     * 调用杭州本地化接口
     * @param request
     * @param url
     * @return
     */
    private String post(Object request,String url){
        try {
            log.info("请求接口url:{},参数:{}",url,JSONObject.toJSONString(request));
            String body = HttpRequest.post(url)
                    .header("Content-Type", "application/json")
                    .header("token","0cc8d63040ec44a19bf57d88acf2b831")
                    .header("X-TRACE-ID", MDC.get( SystemConst.MDC_KEY_TRACE_ID.val()))
                    .body(JSONObject.toJSONString(request))
                    .setConnectionTimeout(10000)
                    .setReadTimeout(10000)
                    .execute()
                    .body();
            log.info("请求接口返回：{}",body);
            JSONObject jsonObject = JSONObject.parseObject(body);
            String response = JSONObject.toJSONString(jsonObject.get("data"));

            log.info("杭州本地化接口返回:{}",response);
            return response;
        } catch (Exception e) {
            log.error("调用杭州本地化接口失败",e);
        }
        return null;

    }
}
